package com._bytecode.__asm._ClassAdapt;

import com._bytecode.__asm.AsmUser;
import com._bytecode.__asm._ClassWriter.MyClassLoader;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;

import static org.objectweb.asm.Opcodes.ASM4;

/**
 * @Author: huangya
 * @Date: 2020/09/16
 * @Description:
 */
public class TransformingClassTest1 {

    public static void main(String[] args) throws IOException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        long start = System.currentTimeMillis();
        System.out.println(start);
        InputStream inputStream = AsmUser.class.getResourceAsStream("AsmUser.class");
        byte[] b1 = new byte[inputStream.available()];
        inputStream.read(b1);

        ClassWriter cw = new ClassWriter(0);
        ClassReader cr = new ClassReader(b1);
        cr.accept(cw, 0);
        byte[] b2 = cw.toByteArray(); // b2 represents the same class as b1

        MyClassLoader myClassLoader = new MyClassLoader();
        Class c = myClassLoader.defineClass("com._bytecode.__asm.AsmUser", b2);
        Object getUserId = c.getDeclaredMethod("getUserId").invoke(c.newInstance());
        System.out.println(getUserId);

        long end = System.currentTimeMillis();
        System.out.println(end);

        System.out.println(end-start);
    }
}
